package com.dss.auto_deploy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dss.auto_deploy.domain.AdmHosts;
import com.dss.auto_deploy.dto.AdmHostsAddDto;
import com.dss.auto_deploy.dto.AdmHostsPageDto;
import com.dss.auto_deploy.dto.AdmHostsUpdateDto;
import com.dss.auto_deploy.mapper.AdmHostsMapper;
import com.dss.auto_deploy.service.IAdmHostsService;
import com.dss.auto_deploy.util.BeanUtils;
import com.dss.auto_deploy.util.JacksonUtils;
import com.dss.auto_deploy.util.StringUtils;
import com.dss.auto_deploy.vo.AdmHostsVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
* @Description:hosts配置管理Service实现类
* @author dongshoushan
* @date 2022-09-26 09:46:02
*/
@Service
@Slf4j
@RequiredArgsConstructor(onConstructor = @_(@Autowired))
public class AdmHostsServiceImpl  extends ServiceImpl<AdmHostsMapper,AdmHosts> implements IAdmHostsService {

    /**
    *
    * hosts配置管理Mapper
    */
    private final AdmHostsMapper admHostsMapper;


    /**
    * 新增hosts配置管理
    * @param admHostsAddDto
    * @return Long
    */
    @Override
    public int insert(AdmHostsAddDto admHostsAddDto){
        AdmHosts admHosts = BeanUtils.copy(admHostsAddDto, AdmHosts.class);
        admHosts.setCreateTime(new Date());
        admHosts.setUpdateTime(new Date());
        int insert = admHostsMapper.insert(admHosts);
        if (insert == 1) {
            log.info("新增成功{}", JacksonUtils.toString(admHosts));
        }
        return insert;
    }


    /**
    * 修改hosts配置管理
    * @param admHostsUpdateDto
    * @return Long
    */
    @Override
    public int update(AdmHostsUpdateDto admHostsUpdateDto){
        AdmHosts admHosts = BeanUtils.copy(admHostsUpdateDto, AdmHosts.class);
        admHosts.setUpdateTime(new Date());
        int number = admHostsMapper.updateById(admHosts);
        if (number == 1) {
            log.info("修改成功{}",JacksonUtils.toString(admHosts));
        }
        return number;
    }


    /**
    * 批量删除hosts配置管理
    * @param ids
    * @return Long
    */
    @Override
    public void deleteBatch(Long[] ids){
        this.removeByIds(Arrays.asList(ids));
    }

    /**
    * hosts配置管理分页查询
    *
    * @param pageDto
    * @return List<AdmHosts>
    */
    @Override
    public Page<AdmHostsVo> list(AdmHostsPageDto pageDto){
        Page<AdmHosts> tablesPage = new Page<>(pageDto.getPage(), pageDto.getLimit());
        QueryWrapper<AdmHosts> queryWrapper = Wrappers.query();
        if (StringUtils.isNotEmpty(pageDto.getContent())){
            queryWrapper.like("host_name",pageDto.getContent());
        }
        queryWrapper.orderByDesc("create_time");
        Page<AdmHosts> pageSelectResult = admHostsMapper.selectPage(tablesPage, queryWrapper);
        List<AdmHosts> records = pageSelectResult.getRecords();
        List<AdmHostsVo> voList = convertToVo(records);
        Page<AdmHostsVo> page = new Page<>();
        page.setRecords(voList);
        page.setTotal(pageSelectResult.getTotal());
        page.setSize(pageSelectResult.getSize());
        return page;
    }

    private List<AdmHostsVo> convertToVo(List<AdmHosts> records) {
        if (records == null || records.isEmpty()){
            return new ArrayList<>();
        }
        return BeanUtils.copyList(records, AdmHostsVo.class);
    }

    @Override
    public List<AdmHostsVo> getAll() {
        List<AdmHosts> admHosts = admHostsMapper.selectList(null);
        return convertToVo(admHosts);
    }

}
